<?php

/**
 * Created by EPP.
 * User: yuxun
 * Date: 18/6/26
 * Time: 21:21
 */
class GiftBagController extends AdminBaseController {
	
	public function index(){
		$this->setTitle('礼包列表');
	
		$filter = new PubFilter();
		$grid   = new PubGrid();
		
		$classifyList = GiftBagService::classifyList();
		$classifyList = array('' => '所有') + $classifyList;
		
		$filter->addFilter('classify', 'select', PubFunc::t('种类'), array('options' => $classifyList, 'value' => key($classifyList)));
		$filter->addFilter('bag_name', 'input', '礼包名称');
        $filter->addFilter('begin', 'input', '创建时间', array('value' => '', 'date' => 'day'));
        $filter->addFilter('end', 'input', '至', array('value' => '', 'date' => 'day'));       
	
		$params = $filter->get();
		$page = $this->R('page', '1');
		$m = $this->R('m');
		$params['m'] = $m;
		$header = GiftBagService::indexHeader();
		if(PubFunc::isAjax()){
			$result = GiftBagService::indexData($params);
			$this->out = array(
					'count'     => $result['count'],
					'data'      => $result['data'],
					'result'    => true
			);
			$this->outType = 'json';
		}else{
			$order  = 'create_time';
			$url = PubFunc::ajaxUrl("?r=giftBag/index", $params);
			$grid->addGrid($header, $url, array(
					'name'=>'数据',
					'order'=>array($order, 'desc'),
					'type'=>'giftBag/index',
					'length'=>10,
					'page'=>$page
			));
	
			$this->out['filter'] = $filter->show();
			$this->out['grid'] = $grid->show();
	
		}
	}
    
    
    public function add(){
    	$this->setTitle('添加礼包');
    	$this->out['classifyList']    = GiftBagService::classifyList();
    }
    
    public function doAdd(){
    	$result = GiftBagService::doAdd($_POST);
    
    	if($result['state']){
    		$this->showMessage('添加成功', PubFunc::buildUrl('/oss/?r=giftBag/index'));
    	}else{
    		$this->showMessage($result['msg']);
    	}
    }
    
    public function delete(){
    	$id = $this->R('id');
    
    	$result = GiftBagService::delete($id);
    
    	if($result !== false){
    		$this->showMessage('删除成功', PubFunc::buildBackUrl("/oss/?r=giftBag/index"));
    	}else{
    		$this->showMessage('删除失败', PubFunc::buildBackUrl("/oss/?r=giftBag/index"));
    	}
    }
    
    

    

}